layui.use(['table', 'jquery', 'layer'], function () {
    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery;
    //表格加载
    var tableIns = table.render({
        elem: '#roleList', //绑定table id
        height: 'full-125',
        url: ctx + '/role/list',
        page: true,
        cellMinWidth: 95, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[ // 表头
            {type: 'checkbox'},
            {type: "numbers"}
            , {field: 'roleName', title: '角色名称'}
            , {field: 'roleDescribition', title: '角色表述'}
            , {field: 'createDate', title: "创建时间"}
            , {field: 'updateDate', title: '更新时间'}
            , {title: '操作', templet: '#currentTableBar', fixed: 'right', minWidth: '150'}
        ]]
    });


    // 监听头部工具栏
    table.on('toolbar(roleFilter)', function (data) {
        if (data.event == 'add') {
            openAddOrUpdateRoleDialog();
        } else if (data.event == 'delete') {
            openDeleteRoleDialog();
        }
    })

    // 添加角色页面
    function openAddOrUpdateRoleDialog(id) {
        var title = "<h3>组织架构 ---- 添加角色</h3>";
        var url = ctx + "/role/toAddOrUpdateRolePage";
        if (id != null && id != '') {
            var title = "<h3>组织架构 ----  修改角色</h3>";
            url = ctx + "/role/toAddOrUpdateRolePage?id=" + id;
        }
        layer.open({
            type: 2,
            title: title,
            area: ['700px', '500px'],
            content: url,
            shadeClose: true,
            maxmin: true
        })
    }

    // 监听行工具栏
    table.on('tool(roleFilter)', function (data) {
        if (data.event == 'edit') {
            //去往编辑页面
            openAddOrUpdateDeptDialog(data.data.id);
        } else if (data.event == 'grant') {
            openGrantPageDialog(data.data.id)
        }
    })

    // 去往授权页面
    function openGrantPageDialog(id) {
        var title = "<h3>组织架构 角色授权  </h3>"
        var url;
        if (id != null && id != '') {
            url = ctx + "/role/toGrantPage?id=" + id;
        }
        layer.open({
            type: 2,
            url: url,
            title: title,
            area: ['800px', '600px'],
            content: url,
            shadeClose: true,
            maxmin: true
        })
    }

    // 删除多条记录
    function openDeleteRoleDialog() {
        var checkStatus = table.checkStatus("roleList");
        var checkRolesData = checkStatus.data;
         console.log(checkRolesData);
        if (checkRolesData.length < 1) {
            layer.msg("未选中数据", {icon: 5})
            return;
        }
        layer.confirm("确认删除选中记录吗", {icon: 3, title: "角色删除"}, function (index) {
                layer.close(index);
                var ids = "ids=";
                for (var i = 0; i < checkRolesData.length; i++) {
                    if (i < checkRolesData.length - 1) {
                        ids = ids + checkRolesData[i].id + "&ids= ";
                    } else {
                        ids = ids + checkRolesData[i].id;
                    }
                }
                $.ajax({
                    type: "post",
                    url: ctx + "/role/deleteBatch",
                    data: ids,
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg("success", {icon: 6});
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg(), {icon: 5})
                        }
                    }
                })

            }
        )

    }


})
